package com.cm.stock.manager.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.UIManager;

import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cm.stock.manager.service.UserService;
import com.cm.stock.manager.utils.CMStockManagerUtils;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.border.CompoundBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;

import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.ArrayList;
import java.util.List;

public class LoginUI extends JFrame{
	private JTextField loginTextField;
	private JTextField pwdTextField;
	private JButton btnConnexion;
	private JLabel lblError;

	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
					CMStockManagerUtils.CONTEXT = new ClassPathXmlApplicationContext("spring-config-mysql.xml");					
					LoginUI window = new LoginUI();					
					window.setVisible(true);
					
//					UserService userService = (UserService) CMStockManagerUtils.CONTEXT.getBean("UserService") ;
//					userService.saveUser("Taponno", "Rodrigue", "77871011", "", "login", "pwd", true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public LoginUI() {
		setTitle("Login");
		setIconImage(Toolkit.getDefaultToolkit().getImage(LoginUI.class.getResource("/com/cm/stock/manager/ui/image/appbar.home.png")));
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		setSize( 400, 250);
		setLocationRelativeTo(null);
		setUndecorated(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setBorder(new CompoundBorder(null, new LineBorder(new Color(51, 153, 255), 4, true)));
		panel.setLayout(null);
		getContentPane().add(panel, BorderLayout.CENTER);
		
		JLabel lblAuthentification = new JLabel("Authentification");
		lblAuthentification.setFont(new Font("Segoe UI Light", Font.BOLD, 26));
		lblAuthentification.setHorizontalAlignment(SwingConstants.CENTER);
		lblAuthentification.setBounds(0, 5, 400, 30);
		panel.add(lblAuthentification);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(10, 46, 380, 2);
		panel.add(separator);
		
		JLabel lblLogin = new JLabel("Login:");
		lblLogin.setBounds(21, 72, 82, 30);
		panel.add(lblLogin);
		
		JLabel lblMotDePasse = new JLabel("Mot De Passe:");
		lblMotDePasse.setBounds(21, 134, 82, 30);
		panel.add(lblMotDePasse);
		
		loginTextField = new JTextField();
		loginTextField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode()==10)
					pwdTextField.requestFocus();
			}
		});
		loginTextField.setBounds(113, 72, 260, 30);
		panel.add(loginTextField);
		loginTextField.setColumns(10);
		
		
		pwdTextField = new JPasswordField();
		pwdTextField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode()==10)
					btnConnexion.requestFocus();
			}
		});
		pwdTextField.setBounds(113, 134, 260, 30);
		panel.add(pwdTextField);
		pwdTextField.setColumns(10);
		
		btnConnexion = new JButton("Connexion");
		btnConnexion.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent arg0) {
				doLogin();
			}

			
		});
		
//		btnConnexion.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent arg0) {
//				doLogin();
//				
//			}
//		});
		btnConnexion.setBounds(273, 190, 100, 40);
		panel.add(btnConnexion);
		
		lblError = new JLabel("");
		lblError.setForeground(Color.RED);
		lblError.setFont(new Font("Segoe UI", Font.PLAIN, 12));
		lblError.setHorizontalAlignment(SwingConstants.CENTER);
		lblError.setBounds(10, 50, 380, 16);
		panel.add(lblError);
	}
	private void doLogin() {

		btnConnexion.setEnabled(false);
		String login = loginTextField.getText();
		String pwd = pwdTextField.getText();
		if(login!=null && pwd != null && login.compareTo("")!=0 && pwd.compareTo("")!=0){
			UserService userService = (UserService) CMStockManagerUtils.CONTEXT.getBean("UserService") ;
			CMStockManagerUtils.CURRENT_USER = userService.doLogin(login, pwd);
			if(CMStockManagerUtils.CURRENT_USER ==null){
				lblError.setText("Erreur: Login ou mot de passe incorrect.");
				btnConnexion.setEnabled(true);
			}else{
				LoginUI.this.dispose();
				new MAinWindow();
			}
		}else{
			lblError.setText("Erreur: Login ou mot de passe obligatoire.");
			btnConnexion.setEnabled(true);
		}
		
	}
}
